/* ============================================================================
* 功能描述 ：主函数
* 作者       ：ggls
* 创建日期 ：2024.04.10
* ----------------------------------------------------------------------------
* 更新历史 ：
*	日期 　　- 修改者   : 更新内容描述
*
* ============================================================================
*/

#include "main.h"
unsigned char RxTable[10],rxDADA;
int time_f;
int now_time_f;

int last_time_f;

bit key_flag;

bit RXOK_f;

static void _data_tx_pack(void)
{
	char app_data[10];
	app_data[0] = 0x01;
	app_data[1] = 0x02;
	app_data[2] = 0x03;
	app_data[3] = 0x04;
	app_data[4] = 0x05;
	app_data[5] = 0x06;
	app_data[6] = 0x06;
	app_data[7] = 0x06;
	app_data[8] = 0x06;
	Uart_Tx_buf(app_data, 9);

}


void main(void)
{
	int i;
	Init_System();

	Uart_Init();
	
	Zigbee_App_Init();
	Zigbee_wake_up();		
	LCD_I2C_init();
	

	WPUA5 = 1;
	TRISA5 = 1;
	
	

	now_time_f = 0;
	key_flag = 0;
	last_time_f = 0;
	time_f = 0;
	Seg_Init();
//	SEG_Init();
	//DelayXms(100);
	//Seg_Display_On();
//	Seg_Display_On();

	Seg_Set_Data();

	DelayXms(500);
	Hly_Temp_Hum_Obtain();

	while(1)
	{
		//Hly_Temp_Hum_Obtain();
		DelayXms(500);
		
		
		app_init();
	
		
		
		
#if 0
		
			RB5 = 0;
		RB4 = 0;
		
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		
		_nop();
		RB5 = ~RB5;
		RB4 = ~RB4;
		_nop();
		_nop();
		
		RB5 = ~RB5;
		RB4 = ~RB4;

		i = TMR1L;
		if (i == 0xff)
		{
		
			_data_tx_pack();
			TMR1L = 0;
			TMR1H = 0;
			time_f++;
		}
			
#endif	

		if(now_time_f == 1)
		{
			_data_tx_pack();
			RB6 = ~RB6;
			now_time_f = 0;
			time_f = 0;
		}
		
		
		

		
#if 0
		if(RXOK_f==1)
		{
			for(i=0;i<8;i++)
			{
					
				TXREG =RxTable[i];

				while(TRMT==0);
			}

			RXOK_f = 0;
		}
#endif
	}
}
void key_on(void)
{
	if(RA5 == 0)
		{
			DelayXms(10);  // 防抖


			if(RA5 == 0)
			{
				key_flag = 1;
			}
			
		}
		else
		{
			DelayXms(10);        
			if(RA5 == 1 && key_flag == 1)   //检测弹起
			{
				//_data_tx_pack();             //弹起后处理任务
				Hly_Temp_Hum_Obtain();
				time_f = 0;
				if(RA5 == 0)
				{
				//	RB6 = 1;
				}
				else
				//	RB6 = 0;
				key_flag = 0;
			}			
		}

}


/***********************************************
函数名称：Init_System
函数功能：系统初始化
入口参数：无
出口参数：无
备注：
************************************************/
void Init_System()
{
	asm("nop");
	asm("clrwdt");
	INTCON = 0;					//系统初始化
	OSCCON = 0X70;				//配置振荡为8M,关WDT
	OPTION_REG = 0;
	
	TMR1ON = 1;
	TMR1IE = 1;
	TMR1GE = 0;
	
	T1CKPS1 = 0;
	T1CKPS0 = 0;
	T1OSCEN = 1;
	
	TMR1CS = 1;
	T1SYNC = 0;

	TMR1L = 0;
	TMR1H = 0;
		
	PEIE = 1;
	RCIE = 1;
	GIE  = 1;

}




/***********************************************
函数名称：PB_Isr
函数功能：PB电平变化中断服务
入口参数：无
出口参数：无
备注：
************************************************/
void interrupt PB_Isr()
{
	if(RBIF)
	{
		RBIF = 0;			//清中断标志
		RB6 = ~RB6;
	
	}
	if (TMR1IF)
	{
		TMR1L = 0;
		TMR1H = 0;
		TMR1IF = 0;
		
		now_time_f =1;
	}

#if 1
	if(RCIF==1)					// 接收中断
	{
		uart_cmd_rx_cplt_callback();
	}

#else
	static unsigned char RxNum=0,TEMP;

	if(RCIF==1)					// 接收中断
	{
		RCIF = 0;
	    
		if(RXOK_f==0)	
		{
			RxTable[RxNum] = RCREG;
			RxNum++;
			if(RxNum > 7)
			{
				RxNum =0;
				RXOK_f =1;
			}
		}
		else
		TEMP =  RCREG;
		
	}
#endif
}


